﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using APILocawebGateway.HTMLHelper;

namespace APILocawebGateway.RedeCard
{
    internal sealed class Utilidades
    {
        public static string FormatCurrencyToRedeCardForm(Decimal currency)
        {
            return FormatCurrency(currency, 10);

        }

        public static string FormatCurrencyToRedeCardConfirmation(Decimal currency)
        {
            String value = FormatCurrency(currency, 15);
            value = value.Insert(value.Length - 2, ".");
            return value;
            //return stringValue.Insert(8, ".");

        }

        private static String FormatCurrency(Decimal currency, Int32 tamanhoCampo)
        {

            String currencyasString = currency.ToString();// (CultureInfo.CreateSpecificCulture("pt-BR").NumberFormat);
            if (currencyasString.Contains(","))
            {
                currencyasString = currencyasString.Remove(currencyasString.IndexOf(",") + 1);
                currencyasString += "00";
            }
            currencyasString = currencyasString.Replace(",", "").Replace(".", "");
            if (currencyasString.Length < tamanhoCampo)
                for (int i = currencyasString.Length; i < tamanhoCampo; i++)
                    currencyasString = "0" + currencyasString;

            return currencyasString;
        }

        public static string RenderRedeCardForm(String pedidoID, String valor)
        {
            Form form = new Form(Configuracao.URLCOMPONENTELOCAWEB, "POST", "frmRedeCard");
            //form.AddControl(new InputHidden("identificacao", LocawebGateway.IDENTIFICACAO));
            form.AddControl(new InputHidden("ambiente", "producao"));
            form.AddControl(new InputHidden("modulo", "REDECARD"));
            form.AddControl(new InputHidden("operacao", "Pagamento"));
            form.AddControl(new InputHidden("bandeira", "MASTERCARD"));
            form.AddControl(new InputHidden("pedido", pedidoID));
            form.AddControl(new InputHidden("valor", FormatCurrencyToRedeCardForm(Convert.ToDecimal(valor))));
            form.AddControl(new InputHidden("parcelas", "01"));
            form.AddControl(new InputHidden("juros", "0"));
            form.AddControl(new PaymentMethodImage("imgRedecard", "/images/icon/ico_mastercard_big.gif", 100, 75));
            form.AddControl(new InputHidden("PAX1", "MASTERCARD"));
            form.AddControl(new InputHidden("transacao", ""));
            form.AddControl(new InputHidden("RedecardIdioma", ""));
            form.AddControl(new InputHidden("TARGET", ""));
            form.AddControl(new InputHidden("urlcima", ""));
            return form.Render();
        }

        public static string RenderDinersRedeCardForm(String pedidoID, String valor)
        {
            Form form = new Form(Configuracao.URLCOMPONENTELOCAWEB, "POST", "frmRedeCard");
            //form.AddControl(new InputHidden("identificacao", LocawebGateway.IDENTIFICACAO));
            form.AddControl(new InputHidden("ambiente", "producao"));
            form.AddControl(new InputHidden("modulo", "REDECARD"));
            form.AddControl(new InputHidden("operacao", "Pagamento"));
            form.AddControl(new InputHidden("bandeira", "DINERS"));
            form.AddControl(new InputHidden("pedido", pedidoID));
            form.AddControl(new InputHidden("valor", FormatCurrencyToRedeCardForm(Convert.ToDecimal(valor))));
            form.AddControl(new InputHidden("parcelas", "01"));
            form.AddControl(new InputHidden("juros", "0"));
            form.AddControl(new PaymentMethodImage("imgRedecard", "/images/icon/ico_dinners_big.gif", 100, 75));
            form.AddControl(new InputHidden("PAX1", "DINNERS"));
            form.AddControl(new InputHidden("transacao", ""));
            form.AddControl(new InputHidden("RedecardIdioma", ""));
            form.AddControl(new InputHidden("TARGET", ""));
            form.AddControl(new InputHidden("urlcima", ""));
            return form.Render();
        }
    }
}
